using System;
using System.Collections.Generic;

namespace SmartBoard.Core.Networking.Client
{
    public class NetworkModuleManager:INetworkModuleManager
    {
        private readonly IDictionary<byte, INetworkServiceModule> _moduleList;

        public NetworkModuleManager()
        {
            _moduleList = new Dictionary<byte, INetworkServiceModule>();    
        }

        #region Implementation of INetworkModuleManager

        public void RegisterModule(INetworkServiceModule module)
        {
            if (this.IsModuleRegistered(module))
                throw new ArgumentException("Module with Id '" + module.ModuleId +"' is already registered.", "module");
            _moduleList[module.ModuleId] = module;
            module.AcceptWriter(DataWriter);
        }

        public void UnregisterModule(INetworkServiceModule module)
        {
            if(this.IsModuleRegistered(module))
            {
                _moduleList.Remove(module.ModuleId);
            }
        }

        public bool IsModuleRegistered(INetworkServiceModule module)
        {
            return _moduleList.ContainsKey(module.ModuleId);
        }

        public INetworkServiceModule GetModule(byte moduleId)
        {
            return _moduleList[moduleId];
        }

        public IClientDataWriter DataWriter
        {
            get; set;
        }

        #endregion
    }
}